package cn.edu.cqytxy.demo1;

/**
 * 运煤车
 *
 * @author yinjw
 */
public class Car {

    private boolean has = false;

    /**
     * 装载
     */
    public synchronized void add() {
        if (has) {
            System.out.println("已装车，等待卸车。。。");
            try {
                wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("正在装车...");
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("装车完毕，开始运输");
            has = true;
            notify();
        }
    }

    /**
     * 卸车
     */
    public synchronized void unload() {
        if (has) {
            System.out.println("正在卸车...");
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("卸车完毕，返回煤场");
            has = false;
            notify();
        } else {
            System.out.println("请先装车。。。");
            try {
                wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
